package com.housesales.web;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;




import module.BasicArea;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import utils.PageUtil;
import bussinessService.basicbussinessService;
import bussinessService.impl.BasicAreaBussinessServiceImpl;
import domain.vo.DistrictQueryObject;
import domain.vo.DistrictVO;
import domain.vo.JSONObject;
import domain.vo.Message;
import domain.vo.queryObject;

/**
 * 
* @ClassName: DistrictController
* @Description: 房屋区域
* @author chenchao
* @date 2013-12-12 上午04:05:23
*
 */
@Controller
@RequestMapping("/district")
public class DistrictController {
	private static Logger logger = Logger.getLogger(DistrictController.class.getName());
	/**
	 * 行政区业务层实现类
	 */
	private basicbussinessService<BasicArea,DistrictVO> baBSI = new BasicAreaBussinessServiceImpl();
	
	@RequestMapping("/viewList")
	public ModelAndView viewList() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("admin/district/district");
		return mv;
	}
	
	@RequestMapping("/viewDistrictsJson")
	public @ResponseBody JSONObject<DistrictVO> viewDistrictsJson(@RequestParam("page") int page,
			@RequestParam("rows") int rows) {
		JSONObject<DistrictVO> jsonObject = new JSONObject<DistrictVO>();
		DistrictQueryObject districtQO = new DistrictQueryObject();
		try {
			int len = baBSI.countByQueryObject(districtQO);
			PageUtil.Page(page, rows);
			districtQO.setStart(PageUtil.start);
			districtQO.setEnd(PageUtil.end);
			List<DistrictVO> tempVOS = new ArrayList<DistrictVO>(); 
			tempVOS=  baBSI.getObjectByQueryObject(districtQO);
			jsonObject.setTotal(len);
			jsonObject.setRows(tempVOS);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	
	@RequestMapping("/viewAllDistrictsJson")
	public @ResponseBody JSONObject<DistrictVO> viewAllDistrictsJson() {
		JSONObject<DistrictVO> jsonObject = new JSONObject<DistrictVO>();
		try {
	
			List<DistrictVO> tempVOS = new ArrayList<DistrictVO>(); 
			tempVOS =  baBSI.getAll();
			
			jsonObject.setTotal(tempVOS.size());
			jsonObject.setRows(tempVOS);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	
	@RequestMapping("/viewDistrictsJsonForForm")
	public @ResponseBody JSONObject<DistrictVO> viewDistrictsJsonForForm() {
		JSONObject<DistrictVO> jsonObject = new JSONObject<DistrictVO>();
		try {
			queryObject queryObject = new DistrictQueryObject();
			List<DistrictVO> tempVOS = new ArrayList<DistrictVO>(); 
			tempVOS =  baBSI.getObjectByQueryObject(queryObject);
			
			jsonObject.setTotal(tempVOS.size());
			jsonObject.setRows(tempVOS);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	
	
	@RequestMapping("/viewTreeDistrictsJson")
	public @ResponseBody JSONObject<DistrictVO> viewTreeDistrictsJson() {
		JSONObject<DistrictVO> jsonObject = new JSONObject<DistrictVO>();
		try {
			queryObject queryObject = new DistrictQueryObject();
			List<DistrictVO> tempVOS = new ArrayList<DistrictVO>(); 
			tempVOS =  baBSI.getObjectByQueryObject(queryObject);
			jsonObject.setTotal(tempVOS.size());
			jsonObject.setRows(tempVOS);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	
	@RequestMapping("/addDistrict")
	public @ResponseBody  Message saveDistrict(@RequestParam("bha_name") String bhaName ) {
		Message ms = new Message();
		try {
			BasicArea ba = new BasicArea();
			ba.setBhaName(bhaName);
			baBSI.merageObject(ba);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			ms.setErrorMsg("数据保存失败");
		}
		return ms;
	}
}
